%generate the variable vector for firing the CM4
F107 = 200;
path(path,'c:\manoj\download\cm4\');
r = 6371.2+400;
Dst = -50;
options.n_min = [ 1 14]; % n_min
options.n_max = [13 45]; % n_max
options.cord = 1;     % geocentric coordinates
options.pred = [1 1 1 1 0 0];
options.filename =  'c:\manoj\download\cm4\umdl.CM4';

theta = [0:2:180];
phi = [0:2:359];
mjd = 450.0;
pp = 1
for i = 1:length(theta),
    for j = 1:length(phi),
      mjd1(pp) = mjd;
      theta1(pp) = theta(i);
      phi1(pp) = phi(j);
      Dst1(pp) = Dst;
      F1071(pp) = F107;
      r1(pp) = r;
      pp = pp+1;
    end;
end;

pp = 1;
for kk = 6:0.5:8,
mjd1(1:end) = 450+kk/24;
[B_x1, B_y1, B_z1] = cm4(mjd1, r1, theta1, phi1, Dst1, F1071, options);
B_x = reshape(B_x1,[length(phi) length(theta) 7]);
B_y = reshape(B_y1,[length(phi) length(theta) 7]);
X(pp,:,:) = squeeze(B_x(:,:,5)+B_x(:,:,6));
Y(pp,:,:) = squeeze(B_y(:,:,5)+B_y(:,:,6));
pp=pp+1;
end;

proj1 = squeeze(B_x(:,:,1))./(sqrt(squeeze(B_x(:,:,1)).^2+ squeeze(B_y(:,:,1)).^2));
proj2 = squeeze(B_y(:,:,1))./(sqrt(squeeze(B_x(:,:,1)).^2+ squeeze(B_y(:,:,1)).^2));
save c:\manoj\projects\eej\CM4EEJ_400 X Y proj1 proj2;
%!shutdown -f
% hf=figure;
% set(hf,'Position',[200   300   800   600]);
% rect = get(hf,'Position'); 
% mov = avifile('eej.avi','fps',1,'quality',100)
% load c:\manoj\projects\tsunami\COLORTABLE;
% load c:\manoj\projects\eej\eej_dip.txt
% for i = 1:24,
% worldmap('world');
% Data = flipud((([squeeze(X(i,:,:)).*proj1+squeeze(Y(i,:,:)).*proj2]')));
% h=contourfm(Data,[0.5,90,0],30);
% plotm(eej_dip(:,2),eej_dip(:,1),'r-');
% colormap(map);
% caxis([-150 150]);
% title(['UT' sprintf('  %d',i)],'FontSize',16);
% colorbar('horiz');
%     F = getframe(hf);
%     mov = addframe(mov,F);
%     pause(0.05);
% end;
% mov = close(mov);
%         
% clear mex % clears all open avi files useful when sharing violation problem occurs  
% 
% 
% 
load('C:\Manoj\projects\tsunami\CHAMP\OVM_1821.dat');
load c:\manoj\projects\eej\CM4EEJ_400.mat;

h=worldmap('world','meshonly');
load coast;
plotm(OVM_1821(15500:17500,2),OVM_1821(15500:17500,3),'y.')
colormap(map);
caxis([-50 50]);
plotm(OVM_1821(10000:12000,2),OVM_1821(10000:12000,3),'r.')
hidem(gca)
gridm

i = 1;
Data = flipud((([squeeze(X(i,:,:)).*proj1+squeeze(Y(i,:,:)).*proj2]')));
h=worldmap('world','meshonly');
plotm(lat,long,'k')
h=contourfm(Data,[0.5,90,0],30);
plotm(OVM_1821(10000:12000,2),OVM_1821(10000:12000,3),'r.');
colormap(map);
caxis([-50 50]);
hidem(gca);
gridm;